package bean;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import entities.KeySeacher;
import entities.VnScoreDoc;
import applicationutil.ApplicationConstant;
import applicationutil.ApplicationUtil;
import luceneutil.V100LunceneWorker;

@ManagedBean(name = "searchBean")
@ViewScoped
public class SearchBean implements Serializable {
	private String textSearch;

	public void doSearch() {
		System.out.println("---do search main");
		V100LunceneWorker worker = new V100LunceneWorker(ApplicationConstant.INDEX_PATH);
		try {
			KeySeacher keySeacher = new KeySeacher(textSearch,1,"","");
			List<VnScoreDoc> listScoreDocs = worker.searchAll(keySeacher);
			/*System.out
					.println("-----------------------in diem ra thu----------------------------------");
			Directory directory;
			IndexSearcher indexSearcher = null;
			try {
				directory = FSDirectory.open(new File(
						ApplicationConstant.INDEX_PATH));
				IndexReader indexReader = DirectoryReader.open(directory);
				indexSearcher = new IndexSearcher(indexReader);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			for (VnScoreDoc s : listScoreDocs) {
				System.out
						.println("----------------------------------------------");
				System.out.println("---doc:" + s.getScoreDoc().doc);

				org.apache.lucene.document.Document document = indexSearcher
						.doc(s.getScoreDoc().doc);
				System.out.println("---pv100:"
						+ document.get(ApplicationConstant.MYPAGE_FIELD_ID));

				System.out.println("---score:" + s.getScoreDoc().score);
				System.out
						.println("----------------------------------------------");
			}*/
			ApplicationUtil.setSession(ApplicationConstant.SCORE_DOCS,
					listScoreDocs);
			ApplicationUtil.setSession(ApplicationConstant.TEXT_SEARCH,
					textSearch);
			ApplicationUtil.navigateTo("result.xhtml");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getTextSearch() {
		return textSearch;
	}

	public void setTextSearch(String textSearch) {
		this.textSearch = textSearch;
	}

}
